package com.apes.scm.settle.payment.model;

import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.metadata.BusinessType;
import com.apes.framework.plugin.soe.store.database.repository.BusinessTypeRepository;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.frame.model.Dept;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = PaymentItem.CONST_TABLE_NAME)
public class PaymentItem extends BaseModel {
    public static final String CONST_TABLE_NAME = "acc_payment_item";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "付款明细表", prefix = "FI")
    @Comment("付款明细编码")
    private String id;

    @NotNull
    @ManyToOne(optional = false)
    @JoinColumn(name = "payment_id", referencedColumnName = "id")
    @Comment("付款单编码")
    private Payment payment; /*付款主表*/

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "payment_execute_id", referencedColumnName = "id")
    @Comment("执行记录编码")
    private PaymentExecuteRecord paymentExecuteRecord;  //执行记录编码

    @Column(name = "amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("付款金额")
    private double amount = 0; //付款金额

    /****  冗余字段，方便与查看 *****/
    @Column(name = "origin_business_type", length = 32, nullable = true)
    @Comment("源单类型")
    private String originBusinessType;

    @Column(name = "origin", length = 30, nullable = true)
    @Comment("源单号")
    private String origin;


    @Column(name = "source", length = 100)
    @Comment("上游单据号码")
    private String source;

    @Column(name = "account_bank" , length = 32)
    @Comment("收款账户")
    private String accountBank;

    @Column(name = "bank", length = 32)
    @Comment("开户行")
    private String bank;

    @Column(name = "bank_account", length = 32)
    @Comment("开户行帐号")
    private String bankAccount;

    @Comment("支行")
    @Column(name = "subbranch", length = 200)
    private String subbranch;


    @Transient
    private String originBusinessName;

    @Transient
    private boolean selected = true;

    public String getOriginBusinessName() {
        return getBusinessTypeName("originBusinessType");
    }

    @Transient
    private Date paymentRegisterApproveDate;    // 付款登记审核日期
    public Date getPaymentRegisterApproveDate(){
        if(this.paymentExecuteRecord != null){
            return this.paymentExecuteRecord.getCreateDate();
        }
        return null;
    }

    @Transient
    private Dept dept;    // 付款登记审核日期
    public Dept getDept(){
        if(this.paymentExecuteRecord != null){
            return this.paymentExecuteRecord.getDept();
        }
        return null;
    }
}
